package cn.stylefeng.guns.modular.business.app;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.business.app.controller.vo.UserVo;
import cn.stylefeng.guns.modular.business.custom.constant.PublicCommon;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
public class RyController {


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;


    /**
     * 获取用户Id
     *
     * @return
     */

    protected UserVo getUserVo() {
        UserVo userVo = new UserVo();
        //获取当前登录用户
        String token = request.getHeader("token");
        if (StrUtil.isEmpty(token)) {
            userVo.setUserId(-1L);
            userVo.setUserType(-1);
            return userVo;
        }
        String tokenStr = redisTemplate.opsForValue().get(token);
        if (StrUtil.isBlank(tokenStr)) {
            userVo.setUserId(-1L);
            userVo.setUserType(-1);
            return userVo;
        }
        userVo.setUserId(Long.valueOf(tokenStr));
        if (token.startsWith(PublicCommon.Token.customer)) {
            userVo.setUserType(PublicCommon.用户);
        } else {
            userVo.setUserType(-1);
        }
        return userVo;
    }

}
